import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import ShowType from '../../../routes/common/showType';
import styles from '../../../index.less';

const FormItem = Form.Item;
class TabBasicInformationComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sAuxiliaryUnit: '', /* 库存单位(*) */
      sConversionUnit: '', /* 换算单位 */
      dPurchasePrice: '', /* 采购价格 */
      dSalesPrice: '', /* 销售价格 */
      dRecentPurchasePrice: '', /* 最近采购价格(*) */
      dDesignateSalesPrice: '', /* 指定销售价格 */
      sSupplyId: '', /* 主要供应商 */
      dProcurementCycle: '', /* 采购周期 */
      sComputeId: '', /* 正算公式 */
      bInverse: '', /* 是否反算 */
      sReComputeId: '', /* 反算公式 */
      sAutoComputeId: '', /* 自动算料 */
      dCoefficient: '', /* 换算系数 */
      sConversionComputeId: '', /* 预留反算公式 */
      sReConversionComputeId: '', /* 预留反算公式Id */
      sId: props.sId, /* 业务数据Id */
      dataList: props.dataSet, /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const { masterConfig, sId } = this.state;
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sAuxiliaryUnit: mainConfig.filter(item => (item.sName === 'sAuxiliaryUnit'))[0],
        sConversionUnit: mainConfig.filter(item => (item.sName === 'sConversionUnit'))[0],
        dPurchasePrice: mainConfig.filter(item => (item.sName === 'dPurchasePrice'))[0],
        dSalesPrice: mainConfig.filter(item => (item.sName === 'dSalesPrice'))[0],
        dRecentPurchasePrice: mainConfig.filter(item => (item.sName === 'dRecentPurchasePrice'))[0],
        dDesignateSalesPrice: mainConfig.filter(item => (item.sName === 'dDesignateSalesPrice'))[0],
        sSupplyId: mainConfig.filter(item => (item.sName === 'sSupplyId'))[0],
        dProcurementCycle: mainConfig.filter(item => (item.sName === 'dProcurementCycle'))[0],
        sComputeId: mainConfig.filter(item => (item.sName === 'sComputeId'))[0],
        bInverse: mainConfig.filter(item => (item.sName === 'bInverse'))[0],
        sReComputeId: mainConfig.filter(item => (item.sName === 'sReComputeId'))[0],
        sAutoComputeId: mainConfig.filter(item => (item.sName === 'sAutoComputeId'))[0],
        dCoefficient: mainConfig.filter(item => (item.sName === 'dCoefficient'))[0],
        sConversionComputeId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sconversioncomputeid'))[0],
        sReConversionComputeId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sreconversioncomputeid'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { disabled, dataSet } = nextProps;
    this.setState({ disabled, dataList: dataSet });
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key && this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  render() {
    const {
      app, form, handleModalPane, handleSelectChange, dataName, dataType, onClose,
    } = this.props;
    const {
      sAuxiliaryUnit, sConversionUnit, dPurchasePrice, dSalesPrice, dRecentPurchasePrice,
    } = this.state;
    const {
      dDesignateSalesPrice, sSupplyId, dProcurementCycle, sComputeId, bInverse,
    } = this.state;
    const {
      sReComputeId, sAutoComputeId, dCoefficient, sConversionComputeId, sReConversionComputeId,
    } = this.state;
    const {
      dataList, sId, sActiveId, disabled,
    } = this.state;
    const childProps = {
      app, form, sActiveId, dataList, sId, disabled, handleModalPane, handleSelectChange, dataName, dataType, onClose,
    };
    const sAuxiliaryUnitProps = { showConfig: sAuxiliaryUnit, ...childProps };
    const sConversionUnitProps = { showConfig: sConversionUnit, ...childProps };
    const dPurchasePriceProps = { showConfig: dPurchasePrice, ...childProps };
    const dSalesPriceProps = { showConfig: dSalesPrice, ...childProps };
    const dRecentPurchasePriceProps = { showConfig: dRecentPurchasePrice, ...childProps };
    const dDesignateSalesPriceProps = { showConfig: dDesignateSalesPrice, ...childProps };
    const sSupplyIdProps = { showConfig: sSupplyId, ...childProps };
    const dProcurementCycleProps = { showConfig: dProcurementCycle, ...childProps };
    const sComputeIdProps = { showConfig: sComputeId, ...childProps };
    const bInverseProps = { showConfig: bInverse, ...childProps };
    const sReComputeIdProps = { showConfig: sReComputeId, ...childProps };
    const sAutoComputeIdProps = { showConfig: sAutoComputeId, ...childProps };
    const dCoefficientProps = { showConfig: dCoefficient, ...childProps };
    const sConversionComputeIdProps = { showConfig: sConversionComputeId, ...childProps };
    const sReConversionComputeIdProps = { showConfig: sReConversionComputeId, ...childProps };
    return (
      <FormItem className={styles.searchSlaveForm}>
        <Row type="flex">
          <Col
            span={6}
            order={sAuxiliaryUnit.iOrder > 24 ? 24 : sAuxiliaryUnit.iOrder}
            style={{ display: sAuxiliaryUnit.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sAuxiliaryUnit" showDefaultName="库存单位" {...sAuxiliaryUnitProps} />
          </Col>
          <Col
            span={6}
            order={sConversionUnit.iOrder > 24 ? 24 : sConversionUnit.iOrder}
            style={{ display: sConversionUnit.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sConversionUnit" showDefaultName="换算单位" {...sConversionUnitProps} />
          </Col>
          <Col
            span={6}
            order={dCoefficient.iOrder > 24 ? 24 : dCoefficient.iOrder}
            style={{ display: dCoefficient.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dCoefficient" showDefaultName="换算系数" {...dCoefficientProps} />
          </Col>
          <Col
            span={6}
            order={sSupplyId.iOrder > 24 ? 24 : sSupplyId.iOrder}
            style={{ display: sSupplyId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sSupplyId" showDefaultName="主要供应商" {...sSupplyIdProps} />
          </Col>
          <Col
            span={6}
            order={dPurchasePrice.iOrder > 24 ? 24 : dPurchasePrice.iOrder}
            style={{ display: dPurchasePrice.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dPurchasePrice" showDefaultName="采购价格" {...dPurchasePriceProps} />
          </Col>
          <Col
            span={6}
            order={dSalesPrice.iOrder > 24 ? 24 : dSalesPrice.iOrder}
            style={{ display: dSalesPrice.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dSalesPrice" showDefaultName="销售价格" {...dSalesPriceProps} />
          </Col>
          <Col
            span={6}
            order={dRecentPurchasePrice.iOrder > 24 ? 24 : dRecentPurchasePrice.iOrder}
            style={{ display: dRecentPurchasePrice.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dRecentPurchasePrice" showDefaultName="最近采购价格" {...dRecentPurchasePriceProps} />
          </Col>
          <Col
            span={6}
            order={dDesignateSalesPrice.iOrder > 24 ? 24 : dDesignateSalesPrice.iOrder}
            style={{ display: dDesignateSalesPrice.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dDesignateSalesPrice" showDefaultName="指定销售价格" {...dDesignateSalesPriceProps} />
          </Col>
          <Col
            span={6}
            order={dProcurementCycle.iOrder > 24 ? 24 : dProcurementCycle.iOrder}
            style={{ display: dProcurementCycle.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dProcurementCycle" showDefaultName="采购周期" {...dProcurementCycleProps} />
          </Col>
          <Col
            span={6}
            order={sComputeId.iOrder > 24 ? 24 : sComputeId.iOrder}
            style={{ display: sComputeId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sComputeId" showDefaultName="正算公式" {...sComputeIdProps} />
          </Col>
          <Col
            span={6}
            order={sReComputeId.iOrder > 24 ? 24 : sReComputeId.iOrder}
            style={{ display: sReComputeId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sReComputeId" showDefaultName="反算公式" {...sReComputeIdProps} />
          </Col>
          <Col
            span={6}
            order={sAutoComputeId.iOrder > 24 ? 24 : sAutoComputeId.iOrder}
            style={{ display: sAutoComputeId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sAutoComputeId" showDefaultName="自动算料" {...sAutoComputeIdProps} />
          </Col>
          <Col
            span={6}
            order={sConversionComputeId.iOrder > 24 ? 24 : sConversionComputeId.iOrder}
            style={{ display: sConversionComputeId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sConversionComputeId" showDefaultName="预留正算公式" {...sConversionComputeIdProps} />
          </Col>
          <Col
            span={6}
            order={sReConversionComputeId.iOrder > 24 ? 24 : sReConversionComputeId.iOrder}
            style={{ display: sReConversionComputeId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sReConversionComputeId" showDefaultName="预留反算公式" {...sReConversionComputeIdProps} />
          </Col>
          <Col
            span={6}
            order={bInverse.iOrder > 24 ? 24 : bInverse.iOrder}
            style={{ display: bInverse.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="bInverse" showDefaultName="是否反算" {...bInverseProps} />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default TabBasicInformationComponent;
